<template>
  <div class="regPage">
    <input type="tel" v-model="userInfo.phoneNumber" placeholder="请输入11位手机号" class="phoneNum">
    <input type="tel" v-model="userInfo.validCode" placeholder="请输入6位短信验证码" class="code">
    <button @click="getValidCode" class="getCode" v-show="!code">获取验证码</button><br>
    <p class="countDown" v-show="code">{{index}}s后重新获取</p>
    <input type="password" v-model="userInfo.password" placeholder="请输入6-12位密码" class="password">
    <input type="checkbox" id="agreeDocument" v-model="userInfo.isAgree" class="radious">
    <label for="agreeDocument">我已同意本条款</label>
    <button @click="register" class="register">注册</button>
  </div>
</template>

<script>
export default {
  name: 'register',
  data () {
    return {
      code: false,
      index: 0,
      userInfo: {
        phoneNumber: '',
        validCode: '',
        password: '',
        isAgree: true
      }
    }
  },
  methods: {
    getValidCode() {
      this.$request.validCode()
        .then(res=>{
          this.$notify({
            message: '验证码：' + res.data.data,
            duration: 5000,
            background: '#1989fa',
          })
        })
        .catch(error=>console.log(error));
      this.code = true;
      this.index = 10;
      let timer = setInterval(() => {
        this.index--;
        if (this.index <= 0) {
          clearInterval(timer);
          this.code = false;
        }
      },1000);
    },
    register () {
      let phpVerify = /^1[34578]\d{9}$/;
      if (this.userInfo.phoneNumber === ''|| this.userInfo.validCode === '' || this.userInfo.password === '') {
        this.$notify({
          message: '请填写账户和密码',
          duration: 2000,
          background: '#1989fa',
        })
      }else if (!phpVerify.test(this.userInfo.phoneNumber)) {
        this.$notify({
          message: '手机号不合法',
          duration: 2000,
          background: '#1989fa',
        })
      }else{
        this.$request.register(this.userInfo)
          .then(res => {
            console.log(res);
            this.$toast.success('注册成功');
            setTimeout( () => {this.$router.push('./login')},2000);
            if (res.data.code === 2003) {
              this.$notify({
                message: res.data.msg,
                duration: 3000,
                background: '#1989fa',
              })
            }
          })
          .catch(error => console.log(error))
      }
    }
  }
}
</script>

<style lang="scss" scoped>

  input{
    width: 85%;
    height: 100px;
    margin-left: 40px;
    border:0;
    border-bottom: 1px solid #eee;
    outline: none;
    padding-left: 20px;
  }
  #agreeDocument{
    width: 20px;
    height: 20px;
    margin-top: 20px;
    display: block;
  }
  label{
    display: inline-block;
    height: 40px;
    font-size: 16px;
    position: absolute;
    top: 670px;
    left: 80px;
  }
  .register{
    width: 75%;
    height: 80px;
    position: absolute;
    left: 0;
    right: 0;
    margin: 82px auto;
    background: #409EFF;
    border: 0;
    border-radius: 50px;
    color: white;
    font-size: 18px;
    outline: none;
    &:active{
      background: #2e8bff;
    }
  }
  .getCode{
    width: 20%;
    height: 50px;
    position: absolute;
    background: #409EFF;
    border: 0;
    border-radius: 8px;
    color: white;
    font-size: 10px;
    outline: none;
    right: 80px;
    top: 470px;
    &:active{
      background: #2e8bff;
    }
  }
  .countDown{
    position: absolute;
    right: 80px;
    top: 455px;
    color: red;
  }
</style>
